package com.emip.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emip.bean.MonitorTypeBean;
import com.emip.dao.MonitorTypeDAO;
import com.emip.service.MonitorTypeService;

@Service("monitorTypeService")
public class MonitorTypeServiceImpl implements MonitorTypeService {

	@Autowired
	MonitorTypeDAO monitorTypeDAO;
	
	public MonitorTypeBean addMonitorType(String typeID, String name) {
		MonitorTypeBean newBean = new MonitorTypeBean();
		newBean.setTypeID(typeID);
		newBean.setName(name);
		//查重，是否已经有相同的typeID的记录存在
		//若有重复记录，则返回null
		if (monitorTypeDAO.findExist(newBean)) {
			return null;
		}
		int newID = monitorTypeDAO.add(newBean);
		newBean.setId(newID);
		return newBean;
	}

	public Boolean deleteMonitorType(int id) {
		MonitorTypeBean delBean = monitorTypeDAO.findByID(id);
		monitorTypeDAO.remove(delBean);
		return true;
	}

	public List<MonitorTypeBean> getAll() {
		return monitorTypeDAO.getAll();
	}

	public MonitorTypeBean getMonitorTypeByID(int id) {
		return monitorTypeDAO.findByID(id);
	}

	public MonitorTypeBean updateMonitorType(int id, String typeID, String name) {
		MonitorTypeBean newBean = monitorTypeDAO.findByID(id);
		newBean.setTypeID(typeID);
		newBean.setName(name);
		//查重，是否已经有相同的typeID的记录存在
		//若有重复记录，则返回null
		if (monitorTypeDAO.findExist(newBean)) {
			return null;
		}
		monitorTypeDAO.update(newBean);
		return newBean;
	}

}
